<%@ page language="java" contentType="text/javascript; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.DecimalFormat"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@page import='java.sql.*' %>
<%@page import="java.util.ArrayList"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONSerializer"%>
<%@page import="net.sf.json.JSONArray"%>

<%
	response.setHeader("Access-Control-Allow-Origin", "*");
	response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
	request.setCharacterEncoding("UTF-8");
	String id = request.getParameter("id");
	String name = request.getParameter("name");
	String title = request.getParameter("title");
	String content = request.getParameter("content");
	
	//String title = new String(request.getParameter("title").getBytes("8859_1"), "utf-8");
	//String content = new String(request.getParameter("content").getBytes("8859_1"), "utf-8");
	//String content = request.getParameter("content");
	//StringReader sreader = new StringReader(content);
	//content = content.replace("\n","<br>");
	//content = content.replace("\u0020","&nbsp;");
	String continent = request.getParameter("continent");
	String nation = request.getParameter("nation");
	
	String date = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
	String time = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date());
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	JSONObject jo = new JSONObject();
	
	try{
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost/itscreater", "itscreater", "campus123");
		
		String insert = "insert into Post" + "(ID, Writer, Title, Content, Continent, Nation, Date, Time) " +
										"values (?, ?, ?, ?, ?, ?, ?, ?);";
		pstmt = conn.prepareStatement(insert);
		pstmt.setString(1, id);
		pstmt.setString(2, name);
		pstmt.setString(3, title);
		//pstmt.setCharacterStream(4, sreader, content.length());
		pstmt.setString(4, content);
		pstmt.setString(5, continent);
		pstmt.setString(6, nation);
		pstmt.setString(7, date);
		pstmt.setString(8, time);
		pstmt.executeUpdate();
		
		jo.put("success", "true");
		jo.put("title",title);
		jo.put("content",content);
		
		out.print(jo);
	} catch (Exception e) {
		jo.put("success", "true");
		jo.put("error",e);
		
		out.print(jo);
	}
	finally{
		try{
			pstmt.close();
		}
		catch (Exception ignored){
		}
		try{
			conn.close();
		}
		catch (Exception ignored){
		}
	}
%>);
